package com.duowan.realtime.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.duowan.datawarehouse.utils.CommonDataCenterLogin;

@Controller
@RequestMapping("/user")
public class UserController {
	
	/**
	 * 开发环境的登录
	 */
	@RequestMapping
	public String loginByDev(HttpServletRequest request, String passport) throws Exception {
//		if(getSystemEnv("DWENV").equalsIgnoreCase("DEV") || getSystemEnv("DWENV").equalsIgnoreCase("TEST")) {
//			return setLoginSucceeInfo(request, passport);
//		}else {
//			throw new SecurityException("只能在开发环境登录");
//		}
		return setLoginSucceeInfo(request, passport);
	}

	private static String getSystemEnv(String key) {
		String value =  System.getenv(key);
		if(StringUtils.isBlank(value)) {
			value = System.getProperty(key);
		}
		return value;
	}
	
	@RequestMapping
	public String loginByDc(ModelMap model, HttpServletRequest request, String passport,String time,String enc) throws Exception {
		
		if(CommonDataCenterLogin.loginByDC(passport, time, enc)) {
			return setLoginSucceeInfo(request, passport);
		}else {
			throw new SecurityException("login error by passport:"+passport);
		}
	}


	private String setLoginSucceeInfo(HttpServletRequest request, String passport) {
		request.getSession().invalidate();
		request.getSession().setAttribute("passport", passport);
		request.setAttribute("passport", passport);
		return "redirect:/index.do";
	}
	
	@RequestMapping
	public String logout(HttpServletRequest request) {
		request.getSession().invalidate();
		return "redirect:https://udb.hiido.com/index.php";
	}
}

